package com.base.library.bindadapter.recyclerView

import android.content.Context
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.*
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

class RecyclerViewExt {
    companion object {
        @JvmStatic
        @BindingAdapter(value = ["recyclerViewLayoutManager"])
        fun setLayoutManager(view: RecyclerView, generator: LayoutGenerator?) {
            generator?.let {
                view.layoutManager = it.createLayoutManager(view.context)
            }
        }

        @JvmStatic
        @BindingAdapter(value = ["adapter"])
        fun setLayoutManager(view: RecyclerView, adapter: RecyclerView.Adapter<out ViewHolder>?) {
            adapter?.let {
                view.adapter = adapter
            }
        }

        @JvmStatic
        @BindingAdapter(value = ["recyclerViewDecoration"])
        fun setDecoration(view: RecyclerView, generator: DecorationGenerator?) {
            generator?.let {
                view.addItemDecoration(it.createLayoutDecoration(view.context))
            }
        }


        @JvmStatic
        fun linearLayoutManager(vertical: Boolean): LayoutGenerator =
            LinearLayoutManagerGenerator(vertical)

        @JvmStatic
        fun linearLayoutManager(): LayoutGenerator =
            linearLayoutManager(true)

        @JvmStatic
        fun gridLayoutManager(
            spanCount: Int
        ): LayoutGenerator = gridLayoutManager(spanCount, LinearLayoutManager.VERTICAL)

        @JvmStatic
        fun gridLayoutManager(
            spanCount: Int, orientation: Int
        ): LayoutGenerator = GridLayoutManagerGenerator(orientation, spanCount)

        @JvmStatic
        fun fleLayoutManager(): LayoutGenerator = FlexLayoutManagerGenerator()

        class LinearLayoutManagerGenerator(var vertical: Boolean) : LayoutGenerator {
            override fun createLayoutManager(context: Context) =
                LinearLayoutManager(context, if (vertical) VERTICAL else HORIZONTAL, false)
        }

        class GridLayoutManagerGenerator(var orientation: Int, var spanCount: Int) :
            LayoutGenerator {
            override fun createLayoutManager(context: Context) =
                GridLayoutManager(context, spanCount, orientation, false)
        }

        class FlexLayoutManagerGenerator() :
            LayoutGenerator {
            override fun createLayoutManager(context: Context) =
                FlexboxLayoutManager(context)
        }

        interface LayoutGenerator {
            fun createLayoutManager(context: Context): RecyclerView.LayoutManager
        }

        interface DecorationGenerator {
            fun createLayoutDecoration(context: Context): RecyclerView.ItemDecoration
        }
    }
}